package com.example.opengl.KText2D;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;

import com.example.opengl.KLog;


public class KSeekView extends GLSurfaceView implements Renderer {
    private KLog kLog = new KLog("KEditSeekView");
    private float oneX;
    private float oneY;
    private boolean mTranslucentBackground;

    private KSeekText2D kSeekText2D;
    private Context kContext;

    public KSeekView(Context context) {
        this(context, null);
        kContext = context;
    }

    public KSeekView(Context context, AttributeSet attrs) {
        super(context, attrs);
        kLog.i("KEditSeekView");
        this.setRenderer(this);
        mTranslucentBackground = true;
        kSeekText2D = new KSeekText2D();

    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        kLog.i("onSurfaceCreated");

//        kSeekText2D.TexCreate(gl);
//        kSeekText2D.loadTexture(gl, R.drawable.backgroundstars, kContext);
        gl.glDisable(GL10.GL_DITHER);

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        if (mTranslucentBackground) {
            gl.glClearColor(0,0,0,0);
        }
        else {
            gl.glClearColor(1,1,1,1);
        }
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
//        gl.glEnable(GL10.GL_TEXTURE_2D);
//        gl.glClearDepthf(1.0f);
//        gl.glEnable(GL10.GL_DEPTH_TEST);
//        gl.glDepthFunc(GL10.GL_LEQUAL);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
//        kLog.i("onSurfaceChanged w = %d h = %d", width, height);
//        oneX = 1 / (float) width;
//        oneY = 1 / (float) height;
//        kLog.i("onSurfaceChanged oneX = %f oneY = %f", oneX, oneY);
//
//        gl.glViewport(0, 0, width,height);
//        gl.glMatrixMode(GL10.GL_PROJECTION);
//        gl.glLoadIdentity();
//
//        gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(0f, 1f, 0f, 1f, -1f, 1f);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//        gl.glMatrixMode(GL10.GL_MODELVIEW);
//        gl.glLoadIdentity();

//        gl.glTranslatef(0.0f,0.0f, -3.0f);
//        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        kSeekText2D.draw(gl);

        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
    }

}
